﻿using Engine.Physic;
using System;
using System.Collections.Generic;

namespace SyneticStandards.Scenery.Grounds
{
    public class GroundsFile
    {
        #region Internal Fields

        internal List<Ground> grounds;

        #endregion Internal Fields

        #region Private Fields

        private const byte VERSION = 0x01;

        #endregion Private Fields

        #region Public Constructors

        public GroundsFile()
        {
            this.grounds = new List<Ground>();
        }

        #endregion Public Constructors

        #region Public Methods

        public List<Ground> GetGrounds()
        {
            return new List<Ground>(this.grounds);
        }

        #endregion Public Methods

        #region Internal Methods

        internal static GroundsFile FromStream(System.IO.BinaryReader br)
        {
            switch (br.ReadByte())
            {
                case 0x01:
                    return LoadV1(br);

                default:
                    throw new Exception("Unsupported Fileformat!!");
            }
        }

        internal void ToStream(System.IO.BinaryWriter bw)
        {
            bw.Write(VERSION);
            bw.Write(this.grounds.Count);
            for (int i = 0; i < this.grounds.Count; i++)
            {
                this.grounds[i].ToStream(bw);
            }
        }

        #endregion Internal Methods

        #region Private Methods

        private static GroundsFile LoadV1(System.IO.BinaryReader br)
        {
            GroundsFile file = new GroundsFile();

            int numberOfGrounds = br.ReadInt32();

            for (int i = 0; i < numberOfGrounds; i++)
            {
                file.grounds.Add(Ground.FromStream(br));
            }

            return file;
        }

        #endregion Private Methods
    }
}